package com.wanhe.shoppingcart.bean;


import com.wanhe.base.model.base.Dish;
import com.wanhe.base.model.base.DishSize;
import com.wanhe.base.model.base.DishTag;
import com.wanhe.base.model.base.SallOutDish;
import com.wanhe.base.model.holder.SallOutHolder;
import com.wanhe.base.util.Logger;
import com.wanhe.base.util.ObjectUtil;
import com.wanhe.shoppingcart.manager.CartManager;
import com.wanhe.shoppingcart.manager.ICalorieCount;
import com.wanhe.shoppingcart.manager.ICartItem;

import java.util.ArrayList;
import java.util.List;

/**
 * 普通菜品的购物车选项
 * Created by Administrator on 2016/5/26.
 */
public class CartItem implements ICartItem, ICalorieCount {

	protected Dish dish;
	protected int count;
	protected String cartItemUuid;
	/**
	 * 口味选择的集合
	 */
	protected List<DishTag> tags;

	/**
	 * 加入到购物车中的时间戳
	 */
	protected long timeStamp;

	/**
	 * 菜品分量的数据
	 */
	protected DishSize size;

	/**
	 * 备注信息
	 */
	protected String remark;

	/**
	 * 是否选择此道菜，（在需求是用listView来显示，item可以选择与否的时候会用到）
	 */
	public boolean isChose;


	@Override
	public String getUUID() {
		return cartItemUuid;
	}

	/**
	 * 同步中间件购物车项的UUID
	 *
	 * @author 陈德基
	 * create at 2017/3/11 0:59
	 */
	public void setCartItemUuid(String uuid) {
		this.cartItemUuid = uuid;
	}

	@Override
	public String getDishGuid() {
		return dish.getDish_guid();
	}

	@Override
	public String getName() {
		return dish.getVch_dishname();
	}

	@Override
	public String getSignID() {
		String id = this.getDishGuid();
		StringBuilder sb = new StringBuilder(id);
		if (ObjectUtil.checkObjectList(tags)) {
			for (int i = 0; i < tags.size(); i++) {
				String tagID = tags.get(i).getID();
				sb.append(tagID);
			}
		}
		if (ObjectUtil.checkObject(size)) {
			sb.append(size.getName());
		}
		Logger.d("=====getSignID==="+sb.toString());
		return sb.toString();
	}

	@Override
	public String getTypeId() {
		return dish.dish_type_guid;
	}

	@Override
	public boolean isSpecial() {
		if (ObjectUtil.checkObjectList(dish.getDish_sizesList())
				|| ObjectUtil.checkObjectList(dish.getDish_tagsList())
				|| dish.isSetmeal()) {
			return true;
		}
		return false;
	}

	@Override
	public int getCount() {
		return this.count;
	}

	@Override
	public double getPrice() {
		double price = 0;

		if (ObjectUtil.checkObject(size)) {
			price = size.getPrice();
		} else {
			price = dish.getNum_price1();
		}

		if (ObjectUtil.checkObjectList(tags)) {
			for (DishTag tag : tags) {
				if (tag != null && tag.getPrice() != null) {
					price += Double.parseDouble(tag.getPrice());
				}
			}
		}

		return price;
	}

	@Override
	public double getVIPPrice() {
		if (ObjectUtil.checkObject(size)) {
			return (size.getPrice2() > 0 ? size.getPrice2() : size.getPrice());
		} else {
			return (dish.getNum_price2() > 0 ? dish.getNum_price2() : dish.getNum_price1());
		}
	}

	@Override
	public void setCount(int count) {
		this.count = count;
	}

	@Override
	public void setAddTime(long timeStamp) {
		this.timeStamp = timeStamp;
	}

	@Override
	public long getAddTime() {
		return timeStamp;
	}

	@Override
	public boolean isSellOut(int count) {
		boolean isSellOut = false;
		SallOutDish sallOutDish = SallOutHolder.getInstance().get(getDish().getCh_dishno());
		if (ObjectUtil.checkObject(sallOutDish)) {
			int itemCount = CartManager.getInstance().getSameGuidItemCount(dish.getDish_guid());
			itemCount += count;
			if (itemCount >= sallOutDish.getWarnNum()) {
				isSellOut = true;
			} else {
				isSellOut = false;
			}
		}
		return isSellOut;
	}


	@Override
	public double getCalorieCount() {
		double calorie = dish.getCalorie() * count;
		return calorie;
	}

	public List<DishTag> getTags() {
		return tags;
	}

	public void setTags(List<DishTag> tags) {
		this.tags = tags;
	}

	public void addTag(DishTag tag) {
		if (tags == null) {
			tags = new ArrayList<>();
		}
		tags.add(tag);
	}

	public DishSize getSize() {
		return size;
	}

	public void setSize(DishSize size) {
		this.size = size;
	}

	public void setDish(Dish dish) {
		this.dish = dish;
	}

	public Dish getDish() {
		return this.dish;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}
}
